package orion.cricScoreCard.Activity;

import orion.cricScoreCard.Entities.CurrentPlayers;
import orion.cricScoreCard.Entities.Match;
import orion.cricScoreCard.Utilities.ScoringFunctions;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ScoreCard extends Activity implements OnClickListener {

	private Button _btnOne;
	private Button _btnTwo;
	private Button _btnZero;
	private Button _btnThree;
	private Button _btnFour;
	private Button _btnSix;
	private Button _btnWide;
	private Button _btnNoBall;
	private Button _btnBye;
	private Button _btnSwep;
	private Button _btnOut;
	private Button _btnUndu;
	private Button _btnSameBall;

	private TextView _txtBattingTeam;
	private TextView _txtOver;
	private TextView _txtScore;
	private TextView _txtStriker;
	private TextView _txtNonStriker;
	private TextView _txtBowler;
	private TextView _txtThisOver1;
	private TextView _txtThisOver2;
	private TextView _txtThisOver3;
	private TextView _txtThisOver4;
	private TextView _txtThisOver5;
	private TextView _txtThisOver6;

	private ImageView imgBall1;
	private ImageView imgBall2;
	private ImageView imgBall3;
	private ImageView imgBall4;
	private ImageView imgBall5;
	private ImageView imgBall6;

	private int counter = 0; // for counting the balls in each over
	private int over = 0;
	private String run = "";

	private static Match _match = new Match();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.score);
		init();
		initContents();
		_btnBye.setOnClickListener(this);
		_btnFour.setOnClickListener(this);
		_btnNoBall.setOnClickListener(this);
		_btnOne.setOnClickListener(this);
		_btnOut.setOnClickListener(this);
		_btnSameBall.setOnClickListener(this);
		_btnSix.setOnClickListener(this);
		_btnSwep.setOnClickListener(this);
		_btnThree.setOnClickListener(this);
		_btnThree.setOnClickListener(this);
		_btnTwo.setOnClickListener(this);
		_btnUndu.setOnClickListener(this);
		_btnWide.setOnClickListener(this);
		_btnZero.setOnClickListener(this);
	}

	private void initContents() {
		_txtBattingTeam.setText(_match.getBattingTeam());
		_txtStriker.setText(CurrentPlayers.getStriker());
		_txtNonStriker.setText(CurrentPlayers.getNon_striker());
		_txtBowler.setText(CurrentPlayers.getBowler());
	}

	private void init() {
		_btnZero = (Button) findViewById(R.id.btnZero);
		_btnOne = (Button) findViewById(R.id.btnOne);
		_btnTwo = (Button) findViewById(R.id.btnTwo);
		_btnThree = (Button) findViewById(R.id.btn3);
		_btnFour = (Button) findViewById(R.id.btn4);
		_btnSix = (Button) findViewById(R.id.btn6);
		_btnWide = (Button) findViewById(R.id.btnWide);
		_btnNoBall = (Button) findViewById(R.id.btnNoBall);
		_btnSwep = (Button) findViewById(R.id.btnSwap);
		_btnBye = (Button) findViewById(R.id.btnByes);
		_btnOut = (Button) findViewById(R.id.btnOut);
		_btnUndu = (Button) findViewById(R.id.btnUndo);
		_btnSameBall = (Button) findViewById(R.id.btnSameBall);

		_txtBattingTeam = (TextView) findViewById(R.id.txtBatting);
		_txtOver = (TextView) findViewById(R.id.txtOvers);
		_txtScore = (TextView) findViewById(R.id.txtTeamScore);
		_txtNonStriker = (TextView) findViewById(R.id.txtNonStrikerBat);
		_txtStriker = (TextView) findViewById(R.id.txtStrikerBat);
		_txtBowler = (TextView) findViewById(R.id.txtBowling);
		_txtThisOver1 = (TextView) findViewById(R.id.txtThisOver1);
		_txtThisOver2 = (TextView) findViewById(R.id.txtThisOver2);
		_txtThisOver3 = (TextView) findViewById(R.id.txtThisOver3);
		_txtThisOver4 = (TextView) findViewById(R.id.txtThisOver4);
		_txtThisOver5 = (TextView) findViewById(R.id.txtThisOver5);
		_txtThisOver6 = (TextView) findViewById(R.id.txtThisOver6);

		imgBall1 = (ImageView) findViewById(R.id.imageBall1);
		imgBall2 = (ImageView) findViewById(R.id.imageBall2);
		imgBall3 = (ImageView) findViewById(R.id.imageBall3);
		imgBall4 = (ImageView) findViewById(R.id.imageBall4);
		imgBall5 = (ImageView) findViewById(R.id.imageBall5);
		imgBall6 = (ImageView) findViewById(R.id.imageBall6);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnOne:
			run = run + "1";
			ScoringFunctions.one();
			ScoringFunctions.swap();
			initContents();
			counter++;
			break;
		case R.id.btnTwo:
			run = run + "2";
			ScoringFunctions.two();
			counter++;
			break;
		case R.id.btn3:
			run = run + "3";
			ScoringFunctions.three();
			ScoringFunctions.swap();
			initContents();
			counter++;
			break;
		case R.id.btn4:
			run = run + "4";
			ScoringFunctions.four();
			counter++;
			break;
		case R.id.btn6:
			run = run + "6";
			ScoringFunctions.six();
			counter++;
			break;
		case R.id.btnByes:
			counter++;
			ScoringFunctions.byes();
			break;
		case R.id.btnNoBall:
			counter++;
			run = run + "NB";
			ScoringFunctions.noball();
			break;
		case R.id.btnOut:
			run = run + "W";
			ScoringFunctions.out();
			counter++;
			break;
		case R.id.btnSameBall:
			counter--;
			ScoringFunctions.sameBall();
			break;
		case R.id.btnSwap:
			ScoringFunctions.swap();
			initContents();
			break;
		case R.id.btnZero:
			run = run + "0";
			ScoringFunctions.zero();
			counter++;
			break;
		case R.id.btnWide:
			run = run + "WD";
			counter++;
			ScoringFunctions.wide();
			break;
		default:
			break;
		}
		setScore();
		setOvers();
		setImageBalls();
	}

	private void GameOver() {
		if (over == ScoringFunctions.overs) {
			counter = 0; // for counting the balls in each over
			over = 0;
			ScoringFunctions.balls = 0;
			ScoringFunctions.wikets = 0;
			ScoringFunctions.runs = 0;
			if (!CurrentPlayers.getFlag()) {
				CurrentPlayers.changeFlag();
				showOptions();
			} else {

			}
		}
	}

	private void setOvers() {
		if (counter == 0) {
			over++;
			ScoringFunctions.swap();
			initContents();
		}
		_txtOver.setText(over + "." + counter + "/" + ScoringFunctions.overs);
		GameOver();
	}

	private void setScore() {
		_txtScore
				.setText(ScoringFunctions.runs + "/" + ScoringFunctions.wikets);
	}

	private void setImageBalls() {
		if (counter == 1) {
			imgBall1.setVisibility(1);
			_txtThisOver1.setText(run);
			run = "";
		} else if (counter == 2) {
			imgBall2.setVisibility(1);
			_txtThisOver2.setText(run);
			run = "";
		} else if (counter == 3) {
			_txtThisOver3.setText(run);
			run = "";
			imgBall3.setVisibility(1);
		} else if (counter == 4) {
			_txtThisOver4.setText(run);
			run = "";
			imgBall4.setVisibility(1);
		} else if (counter == 5) {
			_txtThisOver5.setText(run);
			run = "";
			imgBall5.setVisibility(1);
		} else if (counter == 6) {
			_txtThisOver6.setText(run);
			run = "";
			counter = 0;
			imgBall6.setVisibility(1);
		} else {
			counter = 0;
			imgBall1.setVisibility(View.GONE);
			imgBall2.setVisibility(View.GONE);
			imgBall3.setVisibility(View.GONE);
			imgBall4.setVisibility(View.GONE);
			imgBall5.setVisibility(View.GONE);
			imgBall6.setVisibility(View.GONE);
		}

	}

	public static void setMatch(Match match) {
		_match = match;
	}

	private void showOptions() {
		new AlertDialog.Builder(this)
				.setIcon(R.drawable.ic_launcher)
				.setTitle("CricScore")
				.setMessage("Game Over!!!")
				.setPositiveButton("Continue",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Intent playerInt = new Intent(
										getApplicationContext(),
										TeamDetails.class);
								Bundle data = new Bundle();
								data.putString("Batting",
										_match.getBowlingTeam());
								data.putString("Bowling",
										_match.getBattingTeam());
								data.putString("Overs", _match.getOvers() + "");
								playerInt.putExtras(data);
								startActivity(playerInt);
							}
						}).show();

	}
}
